package com.chj.es.lowlevel;

import org.apache.http.HttpEntity;
import org.apache.http.HttpHost;
import org.apache.http.entity.ContentType;
import org.apache.http.nio.entity.NStringEntity;
import org.apache.http.util.EntityUtils;
import org.elasticsearch.client.Request;
import org.elasticsearch.client.Response;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestClientBuilder;

import java.io.IOException;
import java.util.Collections;
import java.util.Map;

public class TestEsLowSdk {

    public static void main(String[] args) throws IOException {

        RestClient restClient = RestClient.builder(
                new HttpHost("120.168.0.101",9200,"http")
        ).build();

//        RestClientBuilder restClientBuilder =
////        RestClient.builder(
////                new HttpHost("120.24.108.136",9200,"http")
////        );
//        RestClient restClient2 = restClientBuilder.build();

        Map<String,String> params = Collections.EMPTY_MAP;
        String jsonString = "{" +
              "\"msg\":\"Java Low Level REST Client\""+ "}";
        System.out.println(jsonString);

        HttpEntity entity = new NStringEntity(jsonString, ContentType.APPLICATION_JSON);
        Request request = new Request("PUT","enjoy_test/_doc/6");
        request.addParameters(params);
        request.setEntity(entity);

        Response response = restClient.performRequest(request);
        System.out.println(response);
        System.out.println(EntityUtils.toByteArray(response.getEntity()));

        restClient.close();

    }
}
